package com.gxa.p2p.service.impl;

import com.gxa.p2p.mapper.AssetDetailsMapper;
import com.gxa.p2p.pojo.AccountInfo;
import com.gxa.p2p.pojo.AssetDetails;
import com.gxa.p2p.service.AssetDetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * Created with IntelliJ IDEA.
 * User: cxy
 * Date: 2020/08/05 13:30
 * Description:
 * Version: V1.0
 */
@Service("assetDetailsService")
public class AssetDetailsServiceImpl implements AssetDetailsService{

    @Resource
    AssetDetailsMapper assetDetailsMapper;

    /**
     * 处理提现请求
     * @param assetDetails 前端传参   只需要userId
     * @return 装配Assetdetails对象并返回
     */
    @Override
    public AssetDetails getAssetDetails(AssetDetails assetDetails) {
        AssetDetails testassetDetails = assetDetailsMapper.selectByUserId(assetDetails.getUserId());
        AccountInfo accountInfo = assetDetailsMapper.getAccountInfo(assetDetails.getUserId());
        assetDetails.setUnreceivePrincipal(accountInfo.getUnreceivePrincipal());
        assetDetails.setFrozenAmount(accountInfo.getFrozenAmount());
        assetDetails.setUnreturnAmount(accountInfo.getUnreturnAmount());
        assetDetails.setUsableAmount(accountInfo.getUsableAmount());
        assetDetails.setExpense(assetDetailsMapper.getExpense(accountInfo.getUserId()));
        assetDetails.setIncome(assetDetailsMapper.getIncome(accountInfo.getUserId()));
        if (testassetDetails == null) {
            assetDetailsMapper.insert(assetDetails);
        } else {
            assetDetailsMapper.updateByPrimaryKeySelective(assetDetails);
        }
        return assetDetailsMapper.getAssetDetails(assetDetails.getUserId());
    }
}
